package cn.itsource.org.controller;

import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.LoginContext;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@CrossOrigin
//@Controller
//@ResponseBody
@RestController // 等价于  @Controller+@ResponseBody
@RequestMapping("/dept")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;

    //@RequestMapping(value = "/saveOrUpdate",method = RequestMethod.PUT)
    @PutMapping    //访问  http://localhost:8080/dept  请求类型为 put
                                //@RequestBody 接收json数据 并转化为对象
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        System.out.println(department);
        try {
            if (department.getId()!=null){
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
            //就是 操作成功
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据 {"success":false,"message":}
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

    /*
        "/{id}"  在url里面有一个动态的占位符 名字id
        @PathVariable("id") 获取url里面的名字叫做id的占位符数据
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}") // http://localhost:8080/dept/2  、 http://localhost:8080/dept/3 delete请求
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

   // @RequestMapping("/loadById")
    @GetMapping("/{id}")  // http://localhost:8080/dept/2  、 http://localhost:8080/dept/3 get请求
    public Department loadById(@PathVariable("id") Long id){
        try {
            return  departmentService.loadById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //@RequestMapping(value = "/loadAll",method = RequestMethod.GET)
    @GetMapping
    public List<Department> loadAll(){
        try {
            return  departmentService.loadAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //定义分页的方法

    @PostMapping
    public PageResult<Department> queryPage(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(LoginContext.getCurrent(request));
        return departmentService.queryPage(query);
    }


    /*
        前台发送的数据：[10,12,14]

        后台接收数据：
            1.数组 Long[] ids
            2.集合
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
